package tp1.presentacion.controlador;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import tp1.presentacion.modelo.Localidad;
import tp1.presentacion.modelo.LocalidadLogic;
import tp1.presentacion.modelo.TipoVagonLogic;
import tp1.presentacion.modelo.TrenLogic;
import tp1.presentacion.modelo.Viaje;
import tp1.presentacion.modelo.ViajeLogic;

/**
 * Servlet implementation class ViajeController
 */
public class ViajeController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViajeController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String url = request.getPathInfo();
		if(url.equals("/listar")) {
			this.listarBoletos(request, response);
		} else if(url.equals("/nuevo")) {
			response.sendRedirect("nuevoBol.jsp");
		} else if(url.equals("/registrar")) {
			this.registrarBoleto(request, response);
		} else if(url.equals("/comprar")) {
			this.confirmarCompra(request, response);
		}
	}
	
	private void registrarBoleto(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		LocalidadLogic locLog = new LocalidadLogic();
		TrenLogic trLog = new TrenLogic();
		TipoVagonLogic tvLog = new TipoVagonLogic();
		
		int idLocOrigen = Integer.parseInt(request.getParameter("locOrigen"));
		int idLocDestino = Integer.parseInt(request.getParameter("locDestino"));
		int idTren = Integer.parseInt(request.getParameter("tren"));
		int idVagon = Integer.parseInt(request.getParameter("vagon"));
		String dni = request.getParameter("dni");
		String tarj = request.getParameter("tarjeta");
		
		Viaje vi = new Viaje();
		vi.setId(0);
		Localidad origen = locLog.getUno(idLocOrigen);
		vi.setOrigen(origen);
		System.out.println("Origen: "+vi.getOrigen().getNombre());
		Localidad destino = locLog.getUno(idLocDestino);
		vi.setDestino(destino);
		System.out.println("Destino: "+vi.getDestino().getNombre());
		vi.setTren(trLog.getUno(idTren));
		System.out.println("Tren: "+vi.getTren().getNombre());
		vi.setVagon(tvLog.getUno(idVagon));
		System.out.println("Vagon: "+vi.getVagon().getDescripcion());
		vi.setDni(dni);
		vi.setTarjeta(tarj);
		System.out.println("DNI y Tarjeta: "+vi.getDni()+" - "+vi.getTarjeta());
		vi.setFechaEmision(new Date());
		vi.setPrecio(vi.calcularPrecio());
		
		request.getSession().setAttribute("boletoActual", vi);
		RequestDispatcher rdis = request.getRequestDispatcher("/confirmarCompra.jsp");
		rdis.forward(request, response);
	}
	
	private void listarBoletos(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ViajeLogic viLog = new ViajeLogic();
		ArrayList<Viaje> bols = viLog.getTodos();
		request.setAttribute("boletos", bols);
		RequestDispatcher rdis = request.getRequestDispatcher("/listBol.jsp");
		rdis.forward(request, response);		
	}
	
	private void confirmarCompra(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Viaje vi = (Viaje)request.getSession().getAttribute("boletoActual");
		if(vi.getPrecio()!=0) {
			ViajeLogic viLog = new ViajeLogic();
			viLog.agregar(vi);
		}
		request.getSession().removeAttribute("boletoActual");
		response.sendRedirect("index.jsp");
	}
}